{extend name="../../base/view/common/base" /}
<!-- 主体 -->
{block name="body"}
<form class="layui-form gg-form-bar border-t border-x">

    <div class="layui-input-inline" style="width:300px;">
        <input type="text" name="g_orderNum" placeholder="关联单据编号" class="layui-input" autocomplete="off" />
    </div>

    <div class="layui-input-inline" style="width:300px;">
        <input type="text" name="p_name" placeholder="供应商名称" class="layui-input" autocomplete="off" />
    </div>

    <div class="layui-input-inline" style="width:300px;">
        <input type="text" name="md_bar_code" placeholder="产品编码" class="layui-input" autocomplete="off" />
    </div>

    <div class="layui-input-inline" style="width:300px;">
        <input type="text" name="md_name" placeholder="产品名称" class="layui-input" autocomplete="off" />
    </div>
    
    <div class="layui-input-inline" style="width:300px;">
        <input type="text" name="pp_time" placeholder="单据时间" class="layui-input" readonly id="pp_time" autocomplete="off" />
    </div>
    <div class="layui-input-inline" style="width:300px;">
        <input type="text" name="p_type" placeholder="业务类型" class="layui-input" autocomplete="off" />
    </div>
    <div class="layui-input-inline" style="width:300px;">
        <select name="status" >
            {foreach [''=>'全部状态','1'=>'未对账','2'=>'部分对账','3'=>'已对账'] as $k=>$v}
            <option value="{$k}">{$v}</option>
            {/foreach}
        </select>
    </div>

    <div class="layui-input-inline" style="width:150px;">
        <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="webform"><i class="layui-icon layui-icon-search mr-1"></i>搜索</button>
        <button type="reset" class="layui-btn layui-btn-reset" lay-filter="reset">清空</button>
    </div>
</form>
<div class="p-3">
    <table class="layui-hide" id="test" lay-filter="test"></table>
</div>
<script type="text/html" id="toolbarDemo">
  <div class="layui-btn-container">
      <a class="layui-btn layui-btn-sm layui-btn-normal " target="_blank" href="{:url('exportData')}">导出</a>
<!--  	<button class="layui-btn layui-btn-sm addNew" type="button">+ 添加期初</button>-->
<!--  	<button class="layui-btn layui-btn-sm import" type="button">批量导入</button>-->
<!--      <button class="layui-btn layui-btn-sm layui-btn-primary" id="import" >导入</button>-->
<!--      <a href="{__EXPORTMODE__}/期初导入模板.xlsx" download="" class="layui-btn layui-btn-sm layui-btn-primary">导入模板</a>-->
<!--  	<button class="layui-btn layui-btn-sm layui-btn-danger del" lay-event="del" type="button">删除</button>-->
  </div>
</script>
<script type="text/html" id="actionBar">
    <button class="layui-btn layui-btn-normal  layui-btn-xs" lay-event="edit">编辑</button>
<!--    <!--    <button class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</button>-->-->
</script>
{/block}
<!-- /主体 -->

<!-- 脚本 -->
{block name="script"}
	<script>
        var mStatus=['','未对账','部分对账','已对账'];
	const moduleInit = ['tool'];
	function gouguInit() {
		var table = layui.table, tool = layui.tool, form = layui.form,laydate=layui.laydate;
        var upload=layui.upload;
		var tableIns = table.render({
			elem: '#test'
			,toolbar: '#toolbarDemo'
			,defaultToolbar:  ['filter',  'exports']
			,title:'期初档案'
			,url: "{:url('index')}"
			,page: true
			,cellMinWidth: 80
			,cols: [[
                // {type: 'checkbox', fixed: 'left' },
                // {fixed: 'left',field: 'id',title: 'ID' ,unresize: true,hide:true},
					// {field:'id',width:80, title: 'ID号', align:'center'}
					// ,{field:'pCode',title: '供应商编码'}
					{field:'pName',title: '供应商名称'}
					,{field:'bar_code',title: '产品编码'}
					,{field:'mdName',title: '产品名称'}
					,{field:'specs',title: '规格型号'}
					,{field:'unit',title: '单位'}
					,{field:'num',title: '挂账数量'}
					,{field:'dzNum',title: '对账数量'}
					,{field:'type',title: '业务类型'}
					,{field:'g_orderNum',title: '关联单据编号'}
					,{field:'orderDate',title: '日期'}
                    ,{field: 'status', title: '状态',  align: 'center',templet:function (d) {
                        return mStatus[d.status];
                    }}
					// ,{title: '操作',fixed: 'right',width: 200, align:'center', toolbar: '#actionBar'}
				]]
			});
        //监听搜索提交
        form.on('submit(webform)', function (data) {
            console.log('data',data)
            tableIns.reload({
                where: data.field,
                page: {
                    curr: 1
                }
            });
            return false;
        });
			table.on('tool(test)',function (obj) {
				if(obj.event === 'edit'){
                    tool.side("{:url('add')}?id=" + obj.data.id);
				}
				if(obj.event === 'forbid'){
					layer.confirm('确定要禁用该类别吗?', {icon: 3, title:'提示'}, function(index){
						let callback = function (e) {
							layer.msg(e.msg);
							if (e.code == 0) {
                                tableIns.reload();
							}
						}
						tool.post("{:url('reuse')}", { id: obj.data.id,status: 2}, callback);
						layer.close(index);
					});
				}
				if(obj.event === 'resume'){
					layer.confirm('确定要启用该类别吗?', {icon: 3, title:'提示'}, function(index){
						let callback = function (e) {
							layer.msg(e.msg);
							if (e.code == 0) {
                                tableIns.reload();
							}
						}
						tool.post("{:url('reuse')}", { id: obj.data.id,status: 1}, callback);
						layer.close(index);
					});
				}
                if (obj.event === 'del') {
                    layer.confirm('确定要删除吗?', {icon: 3, title: '提示'}, function (index) {
                        let callback = function (e) {
                            layer.msg(e.msg);
                            if (e.code == 0) {
                                obj.del();
                            }
                        }
                        tool.delete("{:url('delete')}", {id: obj.data.id}, callback);
                        layer.close(index);
                    });
                }
			});
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            console.log(checkStatus)
            switch(obj.event){
                case 'add':
                    break;
                case 'del':
                    var data = checkStatus.data;
                    var ids=[]
                    if (data.length){
                        data.forEach(function (value,index) {
                            console.log('value',value)
                            ids.push(value.id)
                        })
                        layer.confirm('确定要删除吗?', {icon: 3, title: '提示'}, function (index) {
                            let callback = function (e) {
                                layer.msg(e.msg);
                                if (e.code == 0) {
                                    tableIns.reload();
                                }
                            }
                            tool.delete("{:url('delete')}", {id: ids.join(',')}, callback);
                            layer.close(index);
                        });

                    }else {
                        layer.msg('请选择数据');
                    }

                    break;
                case 'import':

                    break;

            };
        })
			$('body').on('click','.addNew',function(){
                tool.side("{:url('add')}?");
			});

        var loadingIndex='';
        upload.render({
            elem: '#import'
            ,url: "{:url('importData')}"
            ,accept: 'file'
            ,exts: 'xlsx',
            before:function (res) {
                loadingIndex=layer.load(1,{
                    shade: [0.5,'#a09b9b']
                });
            }
            ,done: function(res){
                layer.msg(res.msg)
                layer.close(loadingIndex)
                setTimeout(function () {
                    location.reload()
                },2000)
            },
            error(e){
                layer.msg('网络异常请稍后再试')
                layer.close(loadingIndex)
            }
        });
        laydate.render({
            elem: '#pp_time'
            ,type:'date',
            range:'~'
        });
	}
	</script>
{/block}
<!-- /脚本 -->